package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import modle.User;

/**
 * Data access object (DAO) for domain model class User.
 * 
 * @see hibernateDao.User
 * @author MyEclipse Persistence Tools
 */

public class UserDao extends BaseDao {
	// property constants
	public static final String USER_NAME = "userName";
	public static final String USER_PASSWORD = "userPassword";
	public static final String IS_AUTO_LOGIN = "isAutoLogin";

	public int delete(User persistentInstance) {
		try {
			String sql = "delete from user where userId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, persistentInstance.getUserId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	public List<User> findAll() {
		try {
			String sql = "select * from user";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public User findById(java.lang.Integer id) {
		try {
			String sql = "select * from user where userId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, id);
			List<User> instanceList = this
					.getListByResultSet(ps.executeQuery());
			if (instanceList == null) {
				return null;
			}
			User instance = instanceList.get(0);
			return instance;
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<User> findByIsAutoLogin(Object isAutoLogin) {
		return findByProperty(IS_AUTO_LOGIN, isAutoLogin);
	}

	public List<User> findByProperty(String propertyName, Object value) {
		try {
			String sql = "select * from user where " + propertyName + "='"
					+ value + "'";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<User> findByUserName(Object userName) {
		return findByProperty(USER_NAME, userName);
	}

	public List<User> findByUserPassword(Object userPassword) {
		return findByProperty(USER_PASSWORD, userPassword);
	}

	@Override
	protected List<User> getListByResultSet(ResultSet rs) {
		List<User> list = new ArrayList<User>();
		try {
			if (!rs.next()) {
				return null;
			}
			do {
				User user = new User();
				user.setUserId(rs.getInt("userId"));
				user.setUserName(rs.getString("userName"));
				user.setUserPassword(rs.getString("userPassword"));
				user.setIsAutoLogin(rs.getByte("isAutoLogin"));
				list.add(user);
			} while (rs.next());
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		}
		return list;
	}

	public int save(User transientInstance) {
		try {
			String sql = "insert into user(userName, userPassword, isAutoLogin) values(?, ?, ?)";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setString(1, transientInstance.getUserName());
			ps.setString(2, transientInstance.getUserPassword());
			ps.setByte(3, transientInstance.getIsAutoLogin());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	@Override
	protected void setSavePreparedStatement(PreparedStatement ps, Object obj)
			throws SQLException {

	}

	public int update(User instance) {
		try {
			String sql = "update user set userName=?, userPassword=?, isAutoLogin=? where userId=?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setString(1, instance.getUserName());
			ps.setString(2, instance.getUserPassword());
			ps.setByte(3, instance.getIsAutoLogin());
			ps.setInt(4, instance.getUserId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}
}